package com.au.bescene.activity;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.au.bescene.R;
import com.au.bescene.util.PatchInputStream;
import com.au.bescene.util.Util;

/**
 * Represents an Event screen.
 * @author Khaled
 *
 */
public class Event extends Activity {
    
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.event);
        
        initializeEvent();
    }
    
    /**
     * Initializes the Event screen. Sets the fonts. Downloads the poster.
     */
    private void initializeEvent() {
        setHeading();
        showPoster();        
    }

    /**
     * Shows the poster.
     */
	private void showPoster() {
		ImageView imageView = (ImageView) findViewById(R.id.event_eventlogo);
		Bundle extras = getIntent().getExtras();
		String eventFolderName = extras.getString("event");
		
        try {
			/* Open a new URL and get the InputStream to load data from it. */
			URL aURL = new URL(getResources().getString(R.string.event_event_posters) + eventFolderName + ".jpg");
			URLConnection conn = aURL.openConnection();
			conn.connect();
			InputStream is = conn.getInputStream();
			PatchInputStream pis = new PatchInputStream(is);
			Bitmap bm = BitmapFactory.decodeStream(pis);
			if(bm == null)
				Log.e("log_tag", "Bitmap = null");
			pis.close();
			is.close();
			
			imageView.setImageBitmap(bm);
		} catch (IOException e) {
			Log.e("log_tag", "Remote Image Exception");
		}		
	}

	/**
	 * Sets event name under the logo.
	 */
	private void setHeading() {
		Bundle extras = getIntent().getExtras();
		String eventFolderName = extras.getString("event");
			
        Typeface futura = Typeface.createFromAsset(getAssets(), "fonts/futura_lt.ttf");
        
        TextView nameOfEvent = (TextView)findViewById(R.id.event_nameofevent);
        nameOfEvent.setTypeface(futura);
        nameOfEvent.setTextSize(30.f);
        nameOfEvent.setText(Util.parseNameToEvent(eventFolderName).getName());		
	}
}